package com.tgu.home_action.controller;

import com.tgu.home_action.pojo.*;
import com.tgu.home_action.service.MarkService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "标记管理")
@RestController
@RequestMapping("/mark")
public class MarkController {
    @Autowired
    private MarkService markService;

    @ApiOperation("志愿者添加标记")
    @PreAuthorize("hasRole('volunteer')")
    @PostMapping("/volunteer/add")
    public ResponseData<Mark> volunteerAdd(@RequestBody @Validated({Mark.insert.class})Mark mark,@ApiIgnore @ModelAttribute("volunteerUser")Volunteer volunteer){
        return markService.volunteerAdd(mark,volunteer);
    }

    @ApiOperation("家属添加标记")
    @PreAuthorize("hasRole('family')")
    @PostMapping("/family/add")
    public ResponseData<Object> familyAdd(@RequestBody @Validated({Mark.insert.class})Mark mark,@ApiIgnore @ModelAttribute("familyUser") Family family){
        return markService.familyAdd(mark,family);
    }

    @ApiOperation("管理员添加标记")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    @PostMapping("/admin/add")
    public ResponseData<Mark> adminAdd(@Validated({Mark.insert.class}) @RequestBody Mark mark,@ApiIgnore @ModelAttribute("adminUser") Admin admin){
        return markService.adminAdd(mark,admin);
    }

    @ApiOperation("志愿者修改标记")
    @PreAuthorize("hasRole('volunteer')")
    @PutMapping("/volunteer/update")
    public ResponseData<Mark> volunteerUpdate(@RequestBody @Validated({Mark.update.class})Mark mark,@ApiIgnore @ModelAttribute("volunteerUser")Volunteer volunteer){
        return markService.volunteerUpdate(mark,volunteer);
    }

    @ApiOperation("家属修改标记")
    @PreAuthorize("hasRole('family')")
    @PutMapping("/family/update")
    public ResponseData<Object> familyUpdate(@RequestBody @Validated({Mark.update.class})Mark mark,@ApiIgnore @ModelAttribute("familyUser") Family family){
        return markService.familyUpdate(mark,family);
    }

    @ApiOperation("管理员修改标记")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    @PutMapping("/admin/update")
    public ResponseData<Object> adminUpdate(@RequestBody @Validated({Mark.update.class})Mark mark,@ApiIgnore @ModelAttribute("adminUser") Admin admin){
        return markService.adminUpdate(mark,admin);
    }

    @ApiOperation("查询标记")
    @GetMapping
    public ResponseData<List<Mark>> get(@RequestBody Mark mark){
        return markService.get(mark);
    }

    @ApiOperation("志愿者删除标记")
    @PreAuthorize("hasRole('volunteer')")
    @DeleteMapping("/volunteer/delete")
    public ResponseData<Object> volunteerDel(@RequestParam Integer markId,@ApiIgnore @ModelAttribute("volunteerUser")Volunteer volunteer){
        return markService.volunteerDel(markId,volunteer);
    }

    @ApiOperation("家属删除标记")
    @PreAuthorize("hasRole('family')")
    @DeleteMapping("/family/delete")
    public ResponseData<Object> familyDel(@RequestParam Integer markId,@ApiIgnore @ModelAttribute("familyUser")Family family){
        return markService.familyDel(markId,family);
    }

    @ApiOperation("管理员删除标记")
    @PreAuthorize("hasAnyRole('admin','superAdmin','districtAdmin')")
    @DeleteMapping("/admin/delete")
    public ResponseData<Object> familyDel(@RequestParam Integer id){
        return markService.adminDel(id);
    }

}
